<template>
  <div class="schools-ranking">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="container">
        <h1>院校排名</h1>
        <p class="subtitle">浙江省高职院校提前招生排行榜</p>
      </div>
    </div>

    <!-- 主要内容 -->
    <div class="container">
      <div class="main-content">
        <!-- 筛选条件 -->
        <el-card class="filter-card" shadow="hover">
          <template #header>
            <h2><el-icon><Filter /></el-icon> 筛选条件</h2>
          </template>
          
          <el-form :model="searchForm" :inline="true" label-width="80px">
            <el-form-item label="排序方式">
              <el-select v-model="searchForm.sortBy" placeholder="选择排序方式" @change="loadRankings">
                <el-option label="综合排名" value="overall" />
                <el-option label="录取率排名" value="admission_rate" />
                <el-option label="报名人数排名" value="application_count" />
                <el-option label="专业数量排名" value="major_count" />
              </el-select>
            </el-form-item>
            <el-form-item label="院校类型">
              <el-select v-model="searchForm.schoolType" placeholder="院校类型" clearable @change="loadRankings">
                <el-option label="高职院校" value="高职院校" />
                <el-option label="技师学院" value="技师学院" />
              </el-select>
            </el-form-item>
            <el-form-item label="所在城市">
              <el-select v-model="searchForm.city" placeholder="选择城市" clearable @change="loadRankings">
                <el-option v-for="city in cities" :key="city" :label="city" :value="city" />
              </el-select>
            </el-form-item>
          </el-form>
        </el-card>

        <!-- 排行榜榜单 -->
        <el-card v-loading="loading" class="ranking-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <h2><el-icon><Trophy /></el-icon> 排行榜</h2>
              <span class="update-time">更新时间：{{ updateTime }}</span>
            </div>
          </template>

          <!-- 前三名特殊展示 -->
          <div v-if="rankings.length > 0" class="top-three">
            <div 
              v-for="(school, index) in rankings.slice(0, 3)" 
              :key="school.id" 
              :class="['top-item', `rank-${index + 1}`]"
              @click="goToDetail(school.id)"
            >
              <div class="rank-badge">
                <el-icon v-if="index === 0" class="medal gold"><Trophy /></el-icon>
                <el-icon v-else-if="index === 1" class="medal silver"><Trophy /></el-icon>
                <el-icon v-else class="medal bronze"><Trophy /></el-icon>
                <span class="rank-number">{{ index + 1 }}</span>
              </div>
              
              <div class="school-avatar">
                <img :src="school.logoUrl || '/default-logo.png'" :alt="school.name" />
              </div>
              
              <div class="school-info">
                <h3>{{ school.name }}</h3>
                <p class="school-type">{{ school.schoolType }}</p>
                <div class="score-info">
                  <span class="score">{{ getRankingScore(school) }}</span>
                  <span class="score-label">{{ getRankingScoreLabel() }}</span>
                </div>
              </div>
              
              <div class="stats">
                <div class="stat-item">
                  <span class="value">{{ school.applicationCount || 0 }}</span>
                  <span class="label">报名人数</span>
                </div>
                <div class="stat-item">
                  <span class="value">{{ school.admissionRate || 0 }}%</span>
                  <span class="label">录取率</span>
                </div>
                <div class="stat-item">
                  <span class="value">{{ school.majorCount || 0 }}</span>
                  <span class="label">专业数</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 完整排行榜表格 -->
          <div class="full-ranking">
            <el-table :data="rankings" stripe>
              <el-table-column label="排名" width="80" align="center">
                <template #default="{ $index }">
                  <div class="rank-cell">
                    <el-icon v-if="$index < 3" class="rank-icon" :class="getRankClass($index)">
                      <Trophy />
                    </el-icon>
                    <span v-else class="rank-text">{{ $index + 1 }}</span>
                  </div>
                </template>
              </el-table-column>
              
              <el-table-column label="院校" min-width="200">
                <template #default="{ row }">
                  <div class="school-cell" @click="goToDetail(row.id)">
                    <img 
                      :src="row.logoUrl || '/default-logo.png'" 
                      :alt="row.name"
                      class="school-logo"
                    />
                    <div class="school-text">
                      <div class="school-name">{{ row.name }}</div>
                      <div class="school-type">{{ row.schoolType }}</div>
                    </div>
                  </div>
                </template>
              </el-table-column>
              
              <el-table-column prop="city" label="所在城市" width="120" />
              
              <el-table-column label="综合得分" width="120" sortable>
                <template #default="{ row }">
                  <span class="score-value">{{ getRankingScore(row) }}</span>
                </template>
              </el-table-column>
              
              <el-table-column prop="applicationCount" label="报名人数" width="120" sortable />
              
              <el-table-column label="录取率" width="100" sortable>
                <template #default="{ row }">
                  <span class="rate-value">{{ row.admissionRate || 0 }}%</span>
                </template>
              </el-table-column>
              
              <el-table-column prop="majorCount" label="专业数量" width="100" sortable />
              
              <el-table-column prop="foundedYear" label="建校时间" width="120" />
              
              <el-table-column label="操作" width="120" fixed="right">
                <template #default="{ row }">
                  <el-button type="primary" size="small" @click="goToDetail(row.id)">
                    查看详情
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>

          <!-- 分页 -->
          <div v-if="pagination.total > 0" class="pagination-wrapper">
            <el-pagination
              v-model:current-page="pagination.page"
              v-model:page-size="pagination.size"
              :total="pagination.total"
              :page-sizes="[20, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="loadRankings"
              @current-change="loadRankings"
            />
          </div>
        </el-card>

        <!-- 排名说明 -->
        <el-card class="explanation-card" shadow="hover">
          <template #header>
            <h2><el-icon><QuestionFilled /></el-icon> 排名说明</h2>
          </template>
          <div class="explanation-content">
            <div class="explanation-section">
              <h3>排名依据</h3>
              <ul>
                <li><strong>综合排名：</strong>基于办学质量、师资力量、就业率、社会声誉等多维度评估</li>
                <li><strong>录取率排名：</strong>根据历年提前招生录取比例进行排序</li>
                <li><strong>报名人数排名：</strong>按照年度提前招生报名人数排序</li>
                <li><strong>专业数量排名：</strong>根据开设的招生专业数量排序</li>
              </ul>
            </div>
            
            <div class="explanation-section">
              <h3>数据来源</h3>
              <p>排名数据来源于浙江省教育考试院官方统计、各院校招生信息及第三方教育评估机构。数据每月更新一次，确保信息的时效性和准确性。</p>
            </div>
            
            <div class="explanation-section">
              <h3>使用建议</h3>
              <p>排名仅供参考，建议考生结合自身兴趣、专业偏好、地理位置等因素综合考虑。选择适合自己的院校和专业才是最重要的。</p>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Filter, 
  Trophy, 
  QuestionFilled 
} from '@element-plus/icons-vue'
import api from '@/api'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const rankings = ref([])
const updateTime = ref('')

const searchForm = reactive({
  sortBy: 'overall',
  schoolType: '',
  city: ''
})

const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

const cities = ref([
  '杭州市', '宁波市', '温州市', '嘉兴市', '湖州市', '绍兴市',
  '金华市', '衢州市', '舟山市', '台州市', '丽水市'
])

// 方法
const loadRankings = async () => {
  loading.value = true
  try {
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...searchForm
    }
    const { data } = await api.get('/schools/ranking', { params })
    rankings.value = data.records || []
    pagination.total = data.total || 0
    updateTime.value = data.updateTime || new Date().toLocaleDateString()
  } catch (error) {
    ElMessage.error('加载排行榜失败')
  } finally {
    loading.value = false
  }
}

const getRankingScore = (school) => {
  switch (searchForm.sortBy) {
    case 'admission_rate':
      return school.admissionRate || 0
    case 'application_count':
      return school.applicationCount || 0
    case 'major_count':
      return school.majorCount || 0
    default:
      return school.overallScore || 0
  }
}

const getRankingScoreLabel = () => {
  switch (searchForm.sortBy) {
    case 'admission_rate':
      return '录取率'
    case 'application_count':
      return '报名数'
    case 'major_count':
      return '专业数'
    default:
      return '综合分'
  }
}

const getRankClass = (index) => {
  if (index === 0) return 'gold'
  if (index === 1) return 'silver'
  if (index === 2) return 'bronze'
  return ''
}

const goToDetail = (schoolId) => {
  router.push(`/schools/${schoolId}`)
}

// 初始化
onMounted(() => {
  loadRankings()
})
</script>

<style lang="scss" scoped>
.schools-ranking {
  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 60px 0;
    text-align: center;
    
    h1 {
      font-size: 2.5rem;
      margin-bottom: 16px;
      font-weight: 600;
    }
    
    .subtitle {
      font-size: 1.2rem;
      opacity: 0.9;
    }
  }
  
  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }
  
  .main-content {
    margin-top: -40px;
    position: relative;
    z-index: 1;
    
    .el-card {
      margin-bottom: 30px;
      border-radius: 12px;
      
      .el-card__header {
        background: #f8f9fa;
        border-bottom: 1px solid #e9ecef;
        
        h2 {
          margin: 0;
          color: #495057;
          display: flex;
          align-items: center;
          gap: 8px;
        }
      }
    }
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .update-time {
      color: #909399;
      font-size: 14px;
    }
  }
  
  .top-three {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
    margin-bottom: 40px;
    
    .top-item {
      text-align: center;
      padding: 24px;
      border-radius: 12px;
      cursor: pointer;
      transition: all 0.3s ease;
      position: relative;
      
      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
      }
      
      &.rank-1 {
        background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
        border: 2px solid #ffd700;
      }
      
      &.rank-2 {
        background: linear-gradient(135deg, #c0c0c0 0%, #e5e5e5 100%);
        border: 2px solid #c0c0c0;
      }
      
      &.rank-3 {
        background: linear-gradient(135deg, #cd7f32 0%, #daa520 100%);
        border: 2px solid #cd7f32;
      }
      
      .rank-badge {
        position: relative;
        margin-bottom: 16px;
        
        .medal {
          font-size: 32px;
          
          &.gold { color: #ffd700; }
          &.silver { color: #c0c0c0; }
          &.bronze { color: #cd7f32; }
        }
        
        .rank-number {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          font-size: 18px;
          font-weight: 600;
          color: white;
          text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }
      }
      
      .school-avatar {
        margin-bottom: 16px;
        
        img {
          width: 80px;
          height: 80px;
          border-radius: 50%;
          object-fit: cover;
          border: 3px solid white;
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
      }
      
      .school-info {
        margin-bottom: 20px;
        
        h3 {
          margin: 0 0 8px 0;
          font-size: 18px;
          font-weight: 600;
          color: #333;
        }
        
        .school-type {
          margin: 0 0 12px 0;
          color: #666;
          font-size: 14px;
        }
        
        .score-info {
          .score {
            display: block;
            font-size: 24px;
            font-weight: 600;
            color: #409eff;
            margin-bottom: 4px;
          }
          
          .score-label {
            font-size: 12px;
            color: #909399;
          }
        }
      }
      
      .stats {
        display: flex;
        justify-content: space-around;
        
        .stat-item {
          text-align: center;
          
          .value {
            display: block;
            font-size: 16px;
            font-weight: 600;
            color: #333;
            margin-bottom: 4px;
          }
          
          .label {
            font-size: 12px;
            color: #666;
          }
        }
      }
    }
  }
  
  .full-ranking {
    .rank-cell {
      display: flex;
      align-items: center;
      justify-content: center;
      
      .rank-icon {
        font-size: 20px;
        
        &.gold { color: #ffd700; }
        &.silver { color: #c0c0c0; }
        &.bronze { color: #cd7f32; }
      }
      
      .rank-text {
        font-size: 16px;
        font-weight: 600;
        color: #606266;
      }
    }
    
    .school-cell {
      display: flex;
      align-items: center;
      cursor: pointer;
      
      &:hover {
        color: #409eff;
      }
      
      .school-logo {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        object-fit: cover;
        margin-right: 12px;
      }
      
      .school-text {
        .school-name {
          font-weight: 500;
          margin-bottom: 4px;
        }
        
        .school-type {
          font-size: 12px;
          color: #909399;
        }
      }
    }
    
    .score-value {
      font-weight: 600;
      color: #409eff;
    }
    
    .rate-value {
      font-weight: 500;
      color: #67c23a;
    }
  }
  
  .pagination-wrapper {
    margin-top: 24px;
    text-align: center;
  }
  
  .explanation-content {
    .explanation-section {
      margin-bottom: 24px;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      h3 {
        margin: 0 0 12px 0;
        color: #303133;
        font-size: 16px;
        font-weight: 600;
      }
      
      ul {
        margin: 0;
        padding-left: 20px;
        
        li {
          margin-bottom: 8px;
          line-height: 1.6;
          color: #606266;
          
          strong {
            color: #303133;
          }
        }
      }
      
      p {
        margin: 0;
        line-height: 1.6;
        color: #606266;
      }
    }
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .schools-ranking {
    .page-header {
      padding: 40px 0;
      
      h1 {
        font-size: 2rem;
      }
      
      .subtitle {
        font-size: 1rem;
      }
    }
    
    .top-three {
      grid-template-columns: 1fr !important;
      gap: 16px;
      
      .top-item {
        padding: 20px;
        
        .school-avatar img {
          width: 60px;
          height: 60px;
        }
        
        .stats {
          .stat-item .value {
            font-size: 14px;
          }
        }
      }
    }
    
    .el-table {
      .school-cell {
        .school-logo {
          width: 32px;
          height: 32px;
          margin-right: 8px;
        }
        
        .school-text .school-name {
          font-size: 14px;
        }
      }
    }
  }
}
</style>